from captcha.fields import CaptchaField, CaptchaTextInput
from django import forms
from django.forms import TextInput

from .models import CustomUser


class UserPassWordChangeForm(forms.Form):
    old_password = forms.CharField(label="原始密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2


class UserLoginForm(forms.Form):
    username = forms.CharField(label="邮箱/用户名", widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入用户名/用户名"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        # "class": "form-control",
        "placeholder": "请输入密码"
    }))


class UserRegisterForm(forms.ModelForm):
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))
    captcha = CaptchaField(label='验证码',widget=CaptchaTextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入验证码"
    }))

    class Meta:
        model = CustomUser
        fields = ['email']
        widgets = {
            'email': forms.EmailInput(attrs={
                "class": "form-control",
                "placeholder": "请输入邮箱"
            }),
        }

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2


class UserInfoChangeForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ['username', 'head']
        widgets = {
            'username': forms.TextInput(attrs={
                "class": "form-control",
                "placeholder": "请输入用户名"
            }),
            'head': forms.FileInput(attrs={})

        },
        labels = {
            'head': "头像"
        }

    def clean_head(self):
        head = self.cleaned_data.get('head')
        return head


class UserHeadChangeForm(forms.Form):
    head = forms.ImageField(label="头像文件", required=False, widget=forms.ClearableFileInput(attrs={
        # "class": "form-control",
        "accept": "image/*"
    }))

    def clean_head(self):
        head = self.cleaned_data.get('head')
        return head


class UserUsernameChangeForm(forms.Form):
    username = forms.CharField(
        label='用户名',
        max_length=150,  # 与 Django User 限制一致
        widget=forms.TextInput(attrs={
            'class': 'form-control',
            'placeholder': '请输入新的用户名',
        })
    )

    def clean_username(self):
        username = self.cleaned_data.get('username', '').strip()
        if not username:
            raise forms.ValidationError('用户名不能为空')
        # 可选：唯一性校验放到视图里带 exclude(pk=...)，因为这里拿不到 request.user
        return username
